package com.neusoft.elmboot.controller;

import com.neusoft.elmboot.po.Business;
import com.neusoft.elmboot.po.User;
import com.neusoft.elmboot.service.AdminService;
import com.neusoft.elmboot.util.response.ResultBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.CrossOrigin;

import java.util.Map;

/**
 * 管理员控制器
 */
@RestController
@RequestMapping("/elm/admin")
@CrossOrigin(origins = "*")
public class AdminController {

    @Autowired
    private AdminService adminService;

    /**
     * 获取系统统计数据
     */
    @PostMapping("/dashboard/stats")
    public ResultBody getDashboardStats() {
        try {
            Map<String, Object> stats = adminService.getDashboardStats();
            return ResultBody.success("获取成功", stats);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultBody.error("获取统计数据失败", null);
        }
    }

    // ==================== 用户管理 ====================

    /**
     * 获取所有用户列表
     */
    @PostMapping("/users/list")
    public ResultBody getUserList(@RequestParam(defaultValue = "1") Integer page,
                                 @RequestParam(defaultValue = "20") Integer size,
                                 @RequestParam(required = false) String keyword) {
        try {
            Map<String, Object> result = adminService.getUserList(page, size, keyword);
            return ResultBody.success("获取成功", result);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultBody.error("获取用户列表失败", null);
        }
    }

    /**
     * 添加用户
     */
    @PostMapping("/users/add")
    public ResultBody addUser(@RequestBody User user) {
        try {
            adminService.addUser(user);
            return ResultBody.success("添加用户成功", null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultBody.error("添加用户失败: " + e.getMessage(), null);
        }
    }

    /**
     * 更新用户信息
     */
    @PostMapping("/users/update")
    public ResultBody updateUser(@RequestBody User user) {
        try {
            adminService.updateUser(user);
            return ResultBody.success("更新用户成功", null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultBody.error("更新用户失败: " + e.getMessage(), null);
        }
    }

    /**
     * 删除用户
     */
    @PostMapping("/users/delete")
    public ResultBody deleteUser(@RequestParam String userId) {
        try {
            adminService.deleteUser(userId);
            return ResultBody.success("删除用户成功", null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultBody.error("删除用户失败: " + e.getMessage(), null);
        }
    }

    /**
     * 更新用户状态
     */
    @PostMapping("/users/status")
    public ResultBody updateUserStatus(@RequestParam String userId, 
                                      @RequestParam Integer status) {
        try {
            adminService.updateUserStatus(userId, status);
            return ResultBody.success("更新用户状态成功", null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultBody.error("更新用户状态失败: " + e.getMessage(), null);
        }
    }

    // ==================== 商家管理 ====================

    /**
     * 获取所有商家列表
     */
    @PostMapping("/businesses/list")
    public ResultBody getBusinessList(@RequestParam(defaultValue = "1") Integer page,
                                     @RequestParam(defaultValue = "20") Integer size,
                                     @RequestParam(required = false) String keyword) {
        try {
            Map<String, Object> result = adminService.getBusinessList(page, size, keyword);
            return ResultBody.success("获取成功", result);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultBody.error("获取商家列表失败", null);
        }
    }

    /**
     * 添加商家
     */
    @PostMapping("/businesses/add")
    public ResultBody addBusiness(@RequestBody Business business) {
        try {
            adminService.addBusiness(business);
            return ResultBody.success("添加商家成功", null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultBody.error("添加商家失败: " + e.getMessage(), null);
        }
    }

    /**
     * 更新商家信息
     */
    @PostMapping("/businesses/update")
    public ResultBody updateBusiness(@RequestBody Business business) {
        try {
            adminService.updateBusiness(business);
            return ResultBody.success("更新商家成功", null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultBody.error("更新商家失败: " + e.getMessage(), null);
        }
    }

    /**
     * 删除商家
     */
    @PostMapping("/businesses/delete")
    public ResultBody deleteBusiness(@RequestParam String businessId) {
        try {
            adminService.deleteBusiness(businessId);
            return ResultBody.success("删除商家成功", null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultBody.error("删除商家失败: " + e.getMessage(), null);
        }
    }

    /**
     * 更新商家状态
     */
    @PostMapping("/businesses/status")
    public ResultBody updateBusinessStatus(@RequestParam String businessId, 
                                          @RequestParam Integer status) {
        try {
            adminService.updateBusinessStatus(businessId, status);
            return ResultBody.success("更新商家状态成功", null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultBody.error("更新商家状态失败: " + e.getMessage(), null);
        }
    }

    // ==================== 订单管理 ====================

    /**
     * 获取所有订单列表
     */
    @PostMapping("/orders/list")
    public ResultBody getOrderList(@RequestParam(defaultValue = "1") Integer page,
                                  @RequestParam(defaultValue = "20") Integer size) {
        try {
            Map<String, Object> result = adminService.getOrderList(page, size);
            return ResultBody.success("获取成功", result);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultBody.error("获取订单列表失败", null);
        }
    }

    /**
     * 获取平台统计数据
     */
    @PostMapping("/statistics")
    public ResultBody getPlatformStatistics() {
        try {
            Map<String, Object> stats = adminService.getPlatformStatistics();
            return ResultBody.success("获取成功", stats);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultBody.error("获取平台统计失败", null);
        }
    }
}
